function querySelector(selector) {
    return document.querySelector(selector) ? document.querySelector(selector) : document.createElement('div')
}
querySelector('#mode').addEventListener('click', function (event) {
    if (querySelector('#mode').checked) {
        assign(prefix + '?path=/' + cur_path + '&mode=view')
    } else {
        assign(prefix + '?path=/' + cur_path + '&mode=list')
    }
}, false)
querySelector('main').addEventListener('click', () => {
    $('#popupRC').css({ display: 'none' });
}, false)
// 新建文件夹
querySelector('#mkdir button').addEventListener('click', event => {
    let el = event.target
    let parent = el.dataset["path"]
    let name = el.parentNode.querySelector('input').value
    axios.post(prefix + '/mkdir', {
        parent: parent,
        name: name,
    })
        .then(function (response) {
            // console.log(response);
            if (response.status === 200) {
                let data = response.data
                if (data['code'] === 0) {
                    console.log('SUCCESS: ' + data['msg'])
                    assign(prefix + '?path=' + data['msg'])
                } else {
                    console.log('FAILED: ' + data['msg'])
                    alert(data['msg'])
                }
            }
        })
        .catch(function (error) {
            console.log(error);
        });
})
document.querySelectorAll('.delete').forEach(el => {
    el.addEventListener('click', function (event) {
        let el = event.target
        while (!el.dataset['path']) {
            el = el.parentNode
        }
        let fullpath = el.dataset["path"]
        let paths = []
        document.querySelectorAll('.fileitem input:checked').forEach(el => {
            let elt = el
            while (!elt.dataset['path']) {
                elt = elt.parentNode
            }
            paths.push(elt.dataset['path'])
        })
        paths = JSON.stringify(paths)
        if (confirm('删除' + paths + '?')) {
            axios.post(prefix + '/delete', {
                paths: paths,
            })
                .then(function (response) {
                    // console.log(response);
                    if (response.status === 200) {
                        let data = response.data
                        if (data['code'] === 0) {
                            console.log('SUCCESS: ' + data['msg'])
                            assign(prefix + '?path=' + fullpath)
                        } else {
                            console.log('FAILED: ' + data['msg'])
                            alert(data['msg'])
                        }
                    }
                })
                .catch(function (error) {
                    console.log(error);
                });
        }
    })
})

querySelector('#popupRC .status').addEventListener('click', function (event) {
    let el = event.target
    let fullpath = el.dataset['path']
    if (!fullpath) return
    axios.get(prefix + '/getitem', {
        params: {
            fullpath: fullpath
        }
    })
        .then(function (response) {
            // console.log(response);
            if (response.status === 200) {
                let data = response.data
                if (data['code'] === 0) {
                    console.log('SUCCESS: ' + data['msg']['path'])
                    querySelector('#status input[name=path]').value = fullpath
                    querySelector('#status select[name=status]').value = data['msg']['status']
                    $('#status').modal('show')
                } else {
                    console.log('FAILED: ' + data['msg'])
                    alert(data['msg'])
                }
            }
        })
        .catch(function (error) {
            console.log(error);
        });
}, false)
// 修改[确定]按钮
querySelector('#status .modal-content button.btn-success').addEventListener('click', function (event) {
    let fullpath = querySelector('#status .modal-content input[name="path"]').value
    let status = querySelector('#status .modal-content select[name="status"]').value
    axios.post(prefix + '/status', {
        fullpath: fullpath,
        status: status,
    })
        .then(function (response) {
            // console.log(response);
            if (response.status === 200) {
                let data = response.data
                if (data['code'] === 0) {
                    console.log('SUCCESS: ' + data['msg'])
                    assign(prefix + '?path=' + data['msg'])
                } else {
                    console.log('FAILED: ' + data['msg'])
                    alert(data['msg'])
                }
            }
        })
        .catch(function (error) {
            console.log(error);
        });
})

querySelector('#popupRC .open').addEventListener('click', function (event) {
    let el = event.target
    let path = el.dataset['path']
    let type = el.dataset['type']
    if (type === 'd') {
        assign(prefix + '?path=' + path)
    }
    if (type === content_type) {
        assign(prefix + '/view?path=' + path)
    }
}, false)

document.querySelectorAll('.move').forEach(el => {
    el.addEventListener('click', function (event) {
        let el = event.target
        let fullpath = el.dataset['path']

        querySelector('#move input[name=newpath]').value = fullpath
        querySelector('#move button').dataset['path'] = fullpath
        $('#move').modal('show')
    }, false)
})

querySelector('#move button').addEventListener('click', function (event) {
    let newpath = querySelector('#move input[name=newpath]').value
    let paths = []
    document.querySelectorAll('.fileitem input:checked').forEach(el => {
        let elt = el
        while (!elt.dataset['path']) {
            elt = elt.parentNode
        }
        paths.push(elt.dataset['path'])
    })
    paths = JSON.stringify(paths)
    axios.post(prefix + '/move', {
        paths: paths,
        newpath: newpath,
    })
        .then(function (response) {
            // console.log(response);
            if (response.status === 200) {
                let data = response.data
                if (data['code'] === 0) {
                    console.log('SUCCESS: ' + data['msg'])
                    assign(prefix + '?path=' + data['msg'])
                } else {
                    console.log('FAILED: ' + data['msg'])
                    alert(data['msg'])
                }
            }
        })
        .catch(function (error) {
            console.log(error);
        });
}, false)
// 导出
document.querySelectorAll('.export').forEach(el => {
    el.addEventListener('click', function (event) {
        let paths = []
        document.querySelectorAll('.fileitem input:checked').forEach(el => {
            let elt = el
            while (!elt.dataset['path']) {
                elt = elt.parentNode
            }
            paths.push(elt.dataset['path'])
        })
        if (paths.length > 0) {
            paths = JSON.stringify(paths)
        } else {
            paths = JSON.stringify([event.target.dataset["path"]])
        }
        axios.get(prefix + '/export', {
            params: {
                paths: paths,
            }
        })
            .then(function (response) {
                // console.log(response);
                if (response.status === 200) {
                    let data = response.data
                    if (data['code'] === 0) {
                        console.log('SUCCESS: ' + data['msg'])
                    } else {
                        console.log('FAILED: ' + data['msg'])
                    }
                    querySelector('#export .modal-content textarea').innerHTML = data['msg']
                    // onpropertychange= "this.style.posHeight=this.scrollHeigh
                    $('#export').modal('show')
                }
            })
            .catch(function (error) {
                console.log(error);
            });
    })
})
// 导入
querySelector('#import button').addEventListener('click', function (event) {
    let data = event.target.parentNode.querySelector('textarea').value
    try {
        data = JSON.stringify(JSON.parse(data))
    } catch (error) {
        alert(error)
    }
    axios.post(prefix + '/itemimport', {
        data: data,
    })
        .then(function (response) {
            // console.log(response);
            if (response.status === 200) {
                let data = response.data
                if (data['code'] === 0) {
                    console.log('SUCCESS: ' + data['msg'])
                    assign(prefix + '?path=/root')
                } else {
                    console.log('FAILED: ' + data['msg'])
                    alert(data['msg'])
                }
            }
        })
        .catch(function (error) {
            console.log(error);
        });
})